<template>
    <transition>
        <div>
            <div>
                <button style="color: #0E76E1;" @click="startRecognize()">扫码</button>
                <button @click="turnLight(light)" style="margin-left: 100px;">开关灯</button>
            </div>
            <div style="position: absolute;bottom: 10px;">
                <div class="fbt" @click="scanPicture()">从相册选择二维码</div>
                <div class="">取消</div>
            </div>
            <div id="bcid">
                <!--盛放扫描控件的div-->
            </div>


        </div>
    </transition>
</template>
<script>
    /* eslint-disable */

    export default {
        name: 'modal',
        mounted() {
            // this.startRecognize();
        },
        data() {
            return {
                light: false,
                scan: {}
            }
        },
        methods: {
            startRecognize() {
                try {
                    let filter;
                    //自定义的扫描控件样式
                    let styles = {frameColor: "#29E52C", scanbarColor: "#29E52C", background: ""}
                    //扫描控件构造
                    this.scan = new plus.barcode.Barcode('bcid', filter, styles);
                    this.scan.onmarked = this.onmarked;
                    this.scan.onerror = this.onerror;
                    this.scan.start();
                } catch (e) {
                    alert("出现错误啦:\n" + e);
                }
            },
            onmarked(type, result) {
                let text = '';
                // switch (type) {
                //     case plus.barcode.QR:
                //         text = 'QR: ';
                //         break;
                //     case plus.barcode.EAN13:
                //         text = 'EAN13: ';
                //         break;
                //     case plus.barcode.EAN8:
                //         text = 'EAN8: ';
                //         break;
                // }
                alert(text + " : " + result);
            },
            onerror(e) {
                alert(e);
            },
            scanPicture() {
                plus.gallery.pick(function (path) {
                    plus.barcode.scan(path, this.onmarked, function (error) {
                        plus.nativeUI.alert("无法识别此图片");
                    });
                }, function (err) {
                    plus.nativeUI.alert("Failed: " + err.message);
                });
            },
            turnLight(bool) {
                this.light = !bool;
                this.scan.setFlash(bool);
            }
        }
    }
</script>
<style type="text/css">
    #bcid {
        width: 100%;
        height: 60%;
        position: absolute;
        top: 10%;
    }


</style>
